/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package com.googlecode.ivybeans.module.queries;

import com.googlecode.ivybeans.facade.ArtifactInfo;
import com.googlecode.ivybeans.module.IvyLibraryController;
import com.googlecode.ivybeans.module.classpath.ClassPathScope;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.event.ChangeListener;

import org.netbeans.api.java.queries.JavadocForBinaryQuery.Result;
import org.netbeans.spi.java.queries.JavadocForBinaryQueryImplementation;
import org.openide.ErrorManager;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.filesystems.URLMapper;
import org.openide.util.ChangeSupport;

/**
 *
 * @author lforet
 */
public class JavadocForBinaryQueryImpl implements JavadocForBinaryQueryImplementation {

    private IvyLibraryController ivyLibraryController;

    public JavadocForBinaryQueryImpl(IvyLibraryController ivyLibraryController) {
        this.ivyLibraryController = ivyLibraryController;
    }

    public Result findJavadoc(URL url) {
//        IOProvider.getDefault().getStdOut().println("findJavadoc for"+url.toString());
        URL binRoot = url;
        if ("jar".equals(url.getProtocol())) { //NOI18N
            binRoot = FileUtil.getArchiveFile(url);
        } else {
            // null for directories.
            return null;
        }
        FileObject jarFO = URLMapper.findFileObject(binRoot);
        if (jarFO != null) {
            File jarFile = FileUtil.toFile(jarFO);
            if (jarFile != null) {
                return new DocResult(getJavadocFile(jarFile));
            }
        }
        return null;
    }

    private File getJavadocFile(File jarFile) {
        for (ArtifactInfo artifactInfo : ivyLibraryController.getResolvedLibrary(ClassPathScope.COMPILE).getArtifacts()) {
            if (jarFile.equals(artifactInfo.getJarFile())) {
                return artifactInfo.getJavadocFile();
            }
        }
        return null;
    }

    public static class DocResult implements Result {

        private File file;
        private final ChangeSupport changeSupport = new ChangeSupport(this);

        public DocResult(File javadoc) {
            file = javadoc;
        }

        public void addChangeListener(ChangeListener changeListener) {
            changeSupport.addChangeListener(changeListener);
        }

        public void removeChangeListener(ChangeListener changeListener) {
            changeSupport.removeChangeListener(changeListener);
        }

        public java.net.URL[] getRoots() {
            if (file != null && file.exists()) {
                try {
                    URL[] urls = new URL[1];
                    urls[0] = FileUtil.getArchiveRoot(file.toURI().toURL());
                    return urls;
                } catch (MalformedURLException exc) {
                    ErrorManager.getDefault().notify(exc);
                }
            }
            return new URL[0];
        }
    }
}
